<?php
namespace app\admin\controller;

use think\facade\Config;
use think\facade\Db;
use think\facade\Filesystem;
/**
 * 文件控制器
 */
class Uploader extends Admin
{
	/**
     * 图片上传, ajax返回
     * @return mixed
     */ 
    public function upload(){
    	date_default_timezone_set("Asia/Shanghai");        
    	//$upload_config = Config::get('upload_driver');
        $upload_config = 'Local';
    	$file = request()->file('file');        
        $info = validate(['image'=>'filesize:10240|fileExt:jpg,png,gif'])
            ->check($file);
        if($upload_config == 'Local'){
            //本地存储图片            
            if($info){
                $data['name'] = $info->getFilename();
                $data['md5'] = $info->hash('sha1');
                $data['sha1'] = $info->hash('md5');
                $data['ext'] = $info->getExtension();
                $data['path'] = '/uploads/' . str_replace('\\', '/', $info->getSaveName());
                $data['location'] = 'location';
                $data['create_time'] = time();
                $data['status'] = 1;
                if($query = Db::name('upload')->Insert($data)){ //插入数据
                    $return['code'] = 1;
                    $return['url'] = $data['path'];
                }else{
                    $return['code'] = 0;
                    $return['msg'] = '上传错误';
                }
            }else{
                //上传失败获取错误信息
                $return['code'] = 0;
                $return['msg'] = '上传错误'.$info;
            }
        }elseif($upload_config == 'Qiniu'){
            //七牛云存储            
        }
        return json($return);
    }
}